<template>
  <div class="login-container">
    <div class="login-form-wrapper">
      <div class="login-form">
        <div class="login-header">
          <h2>后台管理系统</h2>
          <p>欢迎登录</p>
        </div>
        
        <el-form
          ref="loginFormRef"
          :model="loginForm"
          :rules="loginRules"
          class="login-form-content"
          @keyup.enter="handleLogin"
        >
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              placeholder="请输入用户名"
              size="large"
              prefix-icon="User"
              clearable
            />
          </el-form-item>
          
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="请输入密码"
              size="large"
              prefix-icon="Lock"
              show-password
              clearable
            />
          </el-form-item>
          
          <el-form-item>
            <el-checkbox v-model="loginForm.rememberMe">
              记住我
            </el-checkbox>
          </el-form-item>
          
          <el-form-item>
            <el-button
              type="primary"
              size="large"
              :loading="loading"
              class="login-button"
              @click="handleLogin"
            >
              {{ loading ? '登录中...' : '登录' }}
            </el-button>
          </el-form-item>
        </el-form>
        
        <div class="login-tips">
          <p>测试账号：</p>
          <p>管理员：admin / 123456</p>
          <p>普通用户：user / 123456</p>
        </div>

        <div class="login-footer">
          <p>
            还没有账号？
            <span class="register-link" @click="goToRegister">立即注册</span>
            <el-button
              type="text"
              @click="goToRegister"
              style="margin-left: 10px; padding: 0;"
            >
              (点击这里注册)
            </el-button>
          </p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/stores/user'
import { mockLogin } from '@/api/user'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()

const loginFormRef = ref()
const loading = ref(false)

const loginForm = reactive({
  username: 'admin',
  password: '123456',
  rememberMe: false
})

const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 2, max: 20, message: '用户名长度在 2 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  try {
    const valid = await loginFormRef.value.validate()
    if (!valid) return
    
    loading.value = true



    // 使用模拟API进行登录
    const response = await mockLogin(loginForm)
    
    if (response.data.code === 200) {
      await userStore.login(loginForm)
      ElMessage.success('登录成功')
      
      const redirect = route.query.redirect || '/'
      router.push(redirect)
    } else {
      ElMessage.error(response.data.message)
    }
  } catch (error) {
    ElMessage.error('登录失败，请重试')
    console.error('Login error:', error)
  } finally {
    loading.value = false
  }
}

const goToRegister = () => {
  console.log('点击注册按钮，跳转到注册页面')
  router.push('/register')
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.login-form-wrapper {
  width: 100%;
  max-width: 400px;
}

.login-form {
  background: white;
  border-radius: 12px;
  padding: 40px 30px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h2 {
  color: #333;
  margin-bottom: 8px;
  font-size: 24px;
  font-weight: 600;
}

.login-header p {
  color: #666;
  font-size: 14px;
}

.login-form-content {
  margin-bottom: 20px;
}

.login-button {
  width: 100%;
  height: 44px;
  font-size: 16px;
  font-weight: 500;
}

.login-tips {
  text-align: center;
  font-size: 12px;
  color: #999;
  line-height: 1.5;
}

.login-tips p {
  margin: 2px 0;
}

.login-footer {
  text-align: center;
  margin-top: 20px;
  font-size: 14px;
  color: #666;
}

/* 移动端适配 */
@media (max-width: 768px) {
  .login-container {
    padding: 10px;
    min-height: 100vh;
    align-items: stretch;
  }

  .login-form-wrapper {
    width: 100%;
    max-width: none;
    margin: 0;
    display: flex;
    align-items: center;
  }

  .login-form {
    padding: 30px 20px;
    width: 100%;
    margin: 0;
  }

  .login-header h2 {
    font-size: 20px;
  }

  /* 移动端表单优化 */
  :deep(.el-form-item) {
    margin-bottom: 20px;

    .el-input {
      .el-input__inner {
        height: 48px;
        font-size: 16px; /* 防止iOS缩放 */
      }
    }
  }

  .login-button {
    height: 48px;
    font-size: 16px;
  }

  .login-tips {
    font-size: 13px;

    p {
      margin: 4px 0;
    }
  }
}

.register-link {
  color: #409eff;
  cursor: pointer;
  text-decoration: none;
  font-weight: 500;
  transition: color 0.3s;

  &:hover {
    color: #337ecc;
    text-decoration: underline;
  }
}
</style>
